/*--------------------------------------------------------------------------
  Project::MONDIALI SCI ALPINISMO
  Package::
  Class::
  File Defines.h
  --------------------------------------------------------------------------
  * METHODS AND CLASS PARAMETERS


  --------------------------------------------------------------------------
  * FUNCTIONS DESCRIPTION
  
	...

  --------------------------------------------------------------------------
  * COPYRIGHT

  --------------------------------------------------------------------------
  * INFO ABOUT AUTHOR AND VERSIONS
  Author:	Tino Giorgio	Data: 
  Rev1:			Data:

  --------------------------------------------------------------------------
  * E-MAIL & CONTACTS

  GIORGIO TINO:	tinoshi@libero.it
	           	shapeofangels@hotmail.com
	            giorgio.tino@studenti.polito.it

  --------------------------------------------------------------------------*/

#if !defined(DEFINES_H)
#define DEFINES_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define LOG
#define CONSOLE
//#define DEBUG
#include <time.h>
#include <stdio.h>

#define FULLSCREEN

#define COORD_X 0
#define COORD_Y 1
#define COORD_Z 2
#define HEADING 0
#define PITCH	1
#define ROLL	2

#define SCENARIO_FILE_SECTION   "[SCENARIOFILE]"
#define WORLD_FILE_SECTION		"[WORLDFILE]"
#define WORLD_FILE_NAME		    "C:\\Documents and Settings\\Giorgio\\Documenti\\MSA\\world.dem\0"
#define FILE_LIST_SECTION		"[FILES]"
#define MAP_FILE_LIST			"{maps}"
#define WAYPOINT_FILE_LIST		"{waypoints}"
#define CAMERA_SECTION          "[CAMERA]"	
#define OBJECT_LIST_SECTION		"[OBJECTS]"
#define TREES_LIST				"{trees}"
#define BUILDINGS_LIST			"{buildings}"
#define TEXTURES_LIST			"{textures}"
#define FACILITIES_LIST			"{facilities}"

#define OBJECT_TYPE_FILE_NAME "./object_types.ini"
#define BUILDING_TYPES_FILE_NAME "./buildingModels.ini"
#define TREE_TYPES_FILE_NAME    "./treeModels.ini"
#define FACILITY_TYPES_FILE_NAME "./facilityModels.ini"
#define TEXTURE_TYPE_FILE_NAME0 "./citySnow_textures.ini"
#define TEXTURE_TYPE_FILE_NAME1 "./road_textures.ini"

#define TEXTURE_PATH "."
#define TEXTURE_BUTTON_PATH "."
#define BUILDING_MODELS_PATH  "."
#define TREE_MODELS_PATH  "."
#define FACILITY_MODELS_PATH "."

#define START_DIRECTORY "./"//"C:/Documents and Settings/Giorgio/Documenti/MSA/"

#define NOTHING_SELECTED			-1
#define TREES_BUTTON_SELECTED		0
#define BUILDINGS_BUTTON_SELECTED	1
#define TEXTURES_BUTTON_SELECTED	2
#define FACILITIES_BUTTON_SELECTED	3
#define WAYPOINTS_BUTTON_SELECTED	4
#define BULLDOZER_BUTTON_SELECTED	5
#define MOVIE_BUTTON_SELECTED       6

#define newMatrix(pointer,rows,columns,_it_r,type) pointer = (type **) calloc(rows,sizeof(type *)); for (int _it_r = 0; _it_r < rows; _it_r++){ pointer[_it_r] = (type *) calloc(columns,sizeof(type)); }
#define allocMatrix(pointer,rows,columns,_it_r,_it_c,type) for(int _it_r = 0; _it_r < rows; _it_r++) for(int _it_c = 0; _it_c < columns; _it_c++) pointer[_it_r][_it_c] = (type *) malloc(sizeof(type));
#define deleteMatrix(pointer,rows,columns) for(int _j = 0; _j < rows; _j++) {free(pointer[_j]);} free(pointer);
#define printMatrix(pointer,rows,columns) for(int _k = 0; _k < rows; _k++) { for(int _l = 0; _l < columns ; _l++) { printf("%0.1f ",pointer[_k][_l]);} printf("\n");}

#define newTextureMatrix(p,r,c,i,j) p = (GLubyte ***) calloc(r,sizeof(GLubyte **));for(int i=0; i<r; i++) { p[i] = (GLubyte **) calloc(c,sizeof(GLubyte*)); for(int j=0; j<c; j++) {p[i][j] = (GLubyte*)calloc(4,sizeof(GLubyte));p[i][j][0] = p[i][j][1] = p[i][j][2] = p[i][j][3] = 0;} }

#define adjustFileName(x) go = true; while( go ) {if( *(x + i)  == '\n'){*(x+i) = '\0';go = false;} else if( *(x + i) == '\0'){go = false;} else{  i++;} } i=0;

// SG and SSG matrix
#define newSgVecXMatrix(p,r,c,n,i,j) /*float*** p;*/ p = (sgFloat ***) calloc(r,sizeof(sgFloat **));for(int i=0; i<r; i++) { p[i] = (sgFloat **) calloc(c,sizeof(sgFloat*)); for(int j=0; j<c; j++) {p[i][j] = (sgFloat*)calloc(n,sizeof(sgFloat));p[i][j][0] = p[i][j][1] = p[i][j][2] = 0.0f;} }
#define printSgVec3Matrix(p,r,c,n,i,j)  printf("MATRIX STARTS AT [%p]\nROWS [%d]\nCOLS[%d]\n--------------\n\n",p,r,c); for(int i=0; i<r; i++) {for(int j=0; j<c; j++){ printf("[%.2f %.2f %.2f] ",p[i][j][0],p[i][j][1],p[i][j][2]);} printf("\n");} 
#define deleteSgVecXMatrix(p,r,c,i,j)   for(int i=0; i<r; i++) { for(int j=0; j<c; j++){ free(p[i][j]); } free(p[i]); } free(p);
#define normaliseMatrix(p,r,c,i,j) for(int i=0; i<r; i++) for(int j=0; j<c; j++) sgNormaliseVec3(p[i][j]);

#define newSsgTransformMatrix(p,r,c,i,j)    p = (ssgTransform***)calloc(r,sizeof(ssgTransform**));for(int i=0; i<r; i++) { p[i] = (ssgTransform **) calloc(c,sizeof(ssgTransform*)); for(int j = 0; j < c; j++) {p[i][j] = new ssgTransform();} }
#define setSsgTransformMatrix(p,r,c,gs,tgs,i,j) for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { sgVec3 v; sgSetVec3(v,j*gs*tgs,i*gs*tgs,0.0f); (p[i][j])->setTransform(v);}}

#define __printSgMat4(m,i,j) for(int i = 0; i < 4 ; i++) { for(int j = 0; j < 4; j++) { printf("[%.02f]",m[i][j]); } printf("\n");} printf("\n");
#define _printSgMat4(m,i,j,c) __printSgMat4(m,i ## c,j ## c)
#define printSgMat4(m) _printSgMat4(m,i,j,m)

#define __printGLmatrix4(m,i,j) for(int i = 0; i < 4 ; i++) { for(int j = 0; j < 4; j++) { printf("[%.02f]",m[j+4*i]); } printf("\n");} printf("\n");
#define _printGLmatrix4(m,i,j,c) __printGLmatrix4(m,i ## c,j ## c)
#define printGLmatrix4(m) _printGLmatrix4(m,i,j,m)

#define __makeSgMat4double(m,d,i,j) for(int i = 0; i < 4 ; i++) { for(int j = 0; j < 4; j++) { d[i][j] = (double)m[i][j]; };}
#define _makeSgMat4double(m,d,i,j,c) __makeSgMat4double(m,d,i ## c,j ## c)
#define makeSgMat4double(m,d) _makeSgMat4double(m,d,i,j,d)

// Object deletion macro
#define DELETE(x) \
{ \
delete x; \
x = NULL; \
}

#define SAFE_DELETE(x) \
if(x) \
DELETE(x);

// Array deletion macro
#define DELETE_ARRAY(x) \
{ \
delete[] x; \
x = NULL; \
}

#define SAFE_DELETE_ARRAY(x) \
if(x) \
DELETE_ARRAY(x)

// String deletion macro
#define DELETE_STRING(x) DELETE_ARRAY(x)
#define SAFE_DELETE_STRING(x) SAFE_DELETE_ARRAY(x)

// Matrix delete macro
#define DELETE_MATRIX(x) \
{ \
delete[] x[0]; \
delete[] x; \
x = NULL; \
}

#define SAFE_DELETE_MATRIX(x) \
if(x) \
DELETE_MATRIX(x)


// random
#define throwDice(d,n) d = (int)(((float)rand() / ((float)RAND_MAX+1.0f)) * n);

//threads and critical sections
//#ifdef USE_CRITICAL_SECTIONS
//#  define doCritical( x , cs ) try{EnterCriticalSection(cs);x;LeaveCriticalSection(cs);}catch(...){LeaveCriticalSection(cs);}
//#else
#  define doCritical( x , cs ) x;
//#endif

#endif 


